/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
import java.util.*;

public class Semaphore {

    public static Hashtable<Integer,Semaphore> semaphores = new Hashtable();
    public static LinkedList<Integer> semaphoresList = new LinkedList();
    public LinkedList<Integer> procList = new LinkedList();
    public Integer value;
    private Integer id;


    public Semaphore(Integer n){
        value = 1;
        id = n;
        System.out.println("se creo un nuevo semaforo: " + n);
        semaphores.put(n, this);
    }

    public boolean Wait(){
        value--;
        if(value < 0){
            PCB.proc_id.get(Scheduling.procAct).setState(4);
            procList.addLast(new Integer(Scheduling.procAct));
            System.out.println("se realizo el wait: " + this.id + " el proc "+ Scheduling.procAct);
            return true;
        }
        return false;
    }

    public void Signal(){
        value++;
        if(value <= 0){
            PCB.proc_id.get(procList.peek()).setState(2);
            System.out.println("se hizo un signal: " + this.id + " el proc " + procList.peek());
            System.out.println("value: " +value);
            Scheduling.inputProc(PCB.proc_id.get(procList.peek()).queue, procList.poll());
        }
    }
}

